package com.imooc.mq;

import com.imooc.service.ResumeService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author liansen
 * @create 03-15-20:06
 */
@Slf4j
@Component
public class InitResumeMQConsumer {

    @Autowired
    private ResumeService resumeService;

    @RabbitListener(queues = {InitResumeMQConfig.INIT_RESUME_QUEUE})
    public void watchQueue(Message message, Channel channel) throws IOException {

        //获取routing key
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("路由键:" + receivedRoutingKey);
        //收到的内容
        String msg = new String(message.getBody());
        String userId = msg.split(",")[0];
        String msgId = msg.split(",")[1];
        try{
            if(receivedRoutingKey.equalsIgnoreCase(InitResumeMQConfig.INIT_RESUME_ROUTINGKEY)){
                //初始化简历
                resumeService.initResume(userId, msgId);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            }
        }catch(Exception e){
          e.printStackTrace();
          //失败的话会重回队列
          channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
        }
    }
}
